package com.andrew.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ObjectUtils.Null;

import com.andrew.entity.Cart;
import com.andrew.entity.Customer;
import com.andrew.service.CartService;
import com.andrew.service.impl.CartServiceImpl;

public class CountGoodsInCartServlet extends HttpServlet {
	private CartService cartService=new CartServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		if (req.getSession().getAttribute("cart")==null) {
			resp.getWriter().write("0");
			return;
		}
		
		//获取当前用户的信息
		Cart cart=(Cart)req.getSession().getAttribute("cart");
		//获取当前用户购物车中商品的数量
		long count=0;
		try {
			count = cartService.countGoodsInCart(cart);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//输出
		resp.getWriter().write(count+"");
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
